package com.tuozixuan.thymeleaf.demo;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;

import com.tuozixuan.thymeleaf.demo.model.Person;

public class HtmlStringThymeleafDemo {

	public static void main(String[] args) {
		
		StringTemplateResolver templateResolver = new StringTemplateResolver();
		templateResolver.setTemplateMode(TemplateMode.HTML);
		
		TemplateEngine templateEngine = new TemplateEngine();
		templateEngine.setTemplateResolver(templateResolver);
		
		Context context = new Context();
		
		Map<String, Object> map = new HashMap<>();
		map.put("name", "lisi");
		map.put("items", Arrays.asList("one", "two", "three"));
		
		Person person = new Person();
		person.setName("name1");
		
		Person person2 = new Person();
		person2.setName("name2");
		map.put("persons", Arrays.asList(person, person2));
		context.setVariables(map);
		
		// [[]]对应th:text  [()]对应th:utext
		//String result = templateEngine.process("my name is [(${name})]", context);
		
		String templateContent = "<!DOCTYPE HTML>\r\n"
				+ "<html xmlns:th=\"http://www.thymeleaf.org\">\r\n"
				+ "<head>\r\n"
				+ "<title>index</title>\r\n"
				+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\r\n"
				+ "</head>\r\n"
				+ "<body>\r\n"
				+ "	<span th:text=\"${name}\"></span>\r\n"
				+ "	<ul>\r\n"
				+ "  		<li th:each=\"item : ${items}\" th:text=\"${item}\">user name</li>\r\n"
				+ "	</ul>\r\n"
				+ "	<ul>\r\n"
				+ "  		<li th:each=\"person : ${persons}\" th:text=\"${person.name}\">user name</li>\r\n"
				+ "	</ul>"
				+ "</body>\r\n"
				+ "</html>";
		String result = templateEngine.process(templateContent, context);
		System.out.println(result);
	}

}
